package com.intellagric.service.iftmonitor.Impl.ift_network;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftNetworkTypeMapper;
import com.intellagric.pojo.IftNetworkType;
import com.intellagric.pojo.IftNetworkTypeExample;
import com.intellagric.service.iftmonitor.ift_network.NetworkTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 网络类型管理服务实现
 */
@Service
public class NetworkTypeServiceImpl implements NetworkTypeService {

    @Autowired
    private IftNetworkTypeMapper networkTypeMapper;

    /**
     * 添加网络分类
     * @param networkType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addNetworkType(IftNetworkType networkType) {
        if (networkTypeMapper.insertSelective(networkType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除网络分类
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteNetworkType(String id) {
        if (networkTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑网络分类信息
     * @param networkType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editNetworkType(IftNetworkType networkType) {
        if (networkTypeMapper.updateByPrimaryKeySelective(networkType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询网络分类信息
     * @param id
     * @return IftNetworkType
     */
    @Override
    public IftNetworkType getNetworkTypeById(String id) {
        return networkTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询网络分类列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getNetworkTypeList() {
        IftNetworkTypeExample networkTypeExample = new IftNetworkTypeExample();
        networkTypeExample.setOrderByClause("sort");//升序排列
        networkTypeExample.createCriteria().andIdIsNotNull();
        List<IftNetworkType> networkTypeList = networkTypeMapper.selectByExample(networkTypeExample);
        return LayuiDataGridResult.success().add(networkTypeList,networkTypeList.size());
    }

    /**
     * 通过父id查询网络分类信息
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getNetworkTypeByPid(String parentId) {
        IftNetworkTypeExample networkTypeExample = new IftNetworkTypeExample();
        networkTypeExample.setOrderByClause("sort");//升序排列
        networkTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<IftNetworkType> networkTypeList = networkTypeMapper.selectByExample(networkTypeExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(networkTypeList);
        return result;
    }

    /**
     * 查询网络分类信息，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getNetworkTypeTree() {
        //查询所有的分类信息
        List<IftNetworkType> networkTypeList = this.getNetworkTypeList().getData();

        //查询顶分类
        List<IftNetworkType> rootNetworkTypeList = this.getNetworkTypeByPid("0").getData();

        for (IftNetworkType networkType : rootNetworkTypeList) {
            networkType.setChildren(getChild(networkType.getId(),networkTypeList));
        }
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootNetworkTypeList);
        return result;

    }

    /**
     * 递归查询子分类
     * @param id
     * @param networkTypeList
     * @return List<IftEquipmentType>
     */
    private List<IftNetworkType> getChild(String id, List<IftNetworkType> networkTypeList) {
        // 子菜单
        List<IftNetworkType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftNetworkType networkType : networkTypeList) {
            if (networkType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (networkType.getParentId().equals(id)) {
                    childList.add(networkType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (IftNetworkType networkType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            networkType.setChildren(getChild(networkType.getId(), networkTypeList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
